package com.hjj.controller;

import com.hjj.beans.Qa;
import com.hjj.dto.ResultDTO;
import com.hjj.services.QaServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
@RequestMapping("/qa")
public class QaController {
    @Autowired
    QaServices qaServices;

    @RequestMapping(value = "getAll.action", method = RequestMethod.GET)
    public ResultDTO getAll(){
        ArrayList<Qa> all = qaServices.getAll();

        // 封装数据传输对象
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        resultDTO.setReason("查询成功");
        resultDTO.setData(all);

        return resultDTO;
    }

    @RequestMapping(value = "editBefore.action", method = RequestMethod.GET)
    public ResultDTO editBefore(int id){
        Qa qa = qaServices.get(id);

        // 封装数据传输对象
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        resultDTO.setReason("查询成功");
        resultDTO.setData(qa);

        return resultDTO;
    }

    @RequestMapping(value = "add.action", method = RequestMethod.POST)
    public ResultDTO add(@RequestBody Qa qa){
        boolean add = qaServices.add(qa);

        // 封装数据传输对象
        ResultDTO resultDTO = new ResultDTO();

        if (add){
            resultDTO.setResult(ResultDTO.SUCCESS);
            resultDTO.setReason("查询成功");
            resultDTO.setData(add);
        }else {
            resultDTO.setResult(ResultDTO.ERROR);
            resultDTO.setReason("查询失败");
        }

        return resultDTO;
    }

    @RequestMapping(value = "edit.action", method = RequestMethod.POST)
    public ResultDTO edit(@RequestBody Qa qa){
        boolean edit = qaServices.edit(qa);

        // 封装数据传输对象
        ResultDTO resultDTO = new ResultDTO();

        if (edit){
            resultDTO.setResult(ResultDTO.SUCCESS);
            resultDTO.setReason("查询成功");
            resultDTO.setData(edit);
        }else {
            resultDTO.setResult(ResultDTO.ERROR);
            resultDTO.setReason("查询失败");
        }

        return resultDTO;
    }

    @RequestMapping(value = "del.action", method = RequestMethod.GET)
    public ResultDTO del(int id){
        boolean del = qaServices.del(id);

        // 封装数据传输对象
        ResultDTO resultDTO = new ResultDTO();

        if (del){
            resultDTO.setResult(ResultDTO.SUCCESS);
            resultDTO.setReason("查询成功");
            resultDTO.setData(del);
        }else {
            resultDTO.setResult(ResultDTO.ERROR);
            resultDTO.setReason("查询失败");
        }

        return resultDTO;
    }

}
